package com.ruyuan.seckilling.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruyuan.seckilling.activity.entity.Activity;
import com.ruyuan.seckilling.activity.entity.ActivitySkuRef;
import com.ruyuan.seckilling.activity.mapper.ActivitySkuRefMapper;
import com.ruyuan.seckilling.activity.request.ActivitySkuRefModifyRequest;
import com.ruyuan.seckilling.activity.service.ActivitySkuRefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author zhonghuashishan
 */
@Service
public class ActivitySkuRefServiceImpl implements ActivitySkuRefService {

    @Autowired
    private ActivitySkuRefMapper activitySkuRefMapper;

    @Override
    public ActivitySkuRef queryById(Long id) {
        return activitySkuRefMapper.selectById(id);
    }

    @Override
    public List<ActivitySkuRef> queryByActivityId(Long activityId) {
        return queryByActivityIds(Collections.singletonList(activityId));
    }

    @Override
    public List<ActivitySkuRef> queryByActivityIds(List<Long> activityIds) {
        QueryWrapper<ActivitySkuRef> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("activity_id", activityIds);
        return activitySkuRefMapper.selectList(queryWrapper);
    }

    @Override
    public Integer countByActivityIdAndSkuId(Long activityId, Long skuId) {
        QueryWrapper<ActivitySkuRef> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id", activityId);
        queryWrapper.eq("sku_id", skuId);
        return activitySkuRefMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer countByActivityIdAndPageReady(Long activityId, boolean pageReady) {
        QueryWrapper<ActivitySkuRef> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id", activityId);
        queryWrapper.eq("page_ready", pageReady);
        return activitySkuRefMapper.selectCount(queryWrapper);
    }

    @Override
    public void save(ActivitySkuRef activitySkuRef) {
        activitySkuRef.setCreateTime(new Date());
        activitySkuRef.setUpdateTime(activitySkuRef.getCreateTime());
        activitySkuRefMapper.insert(activitySkuRef);
    }

    @Override
    public void modify(ActivitySkuRefModifyRequest request) {
        ActivitySkuRef.ActivitySkuRefBuilder builder = ActivitySkuRef.builder().id(request.getActivitySkuRefId());
        if (Objects.nonNull(request.getPrice())) {
            builder.price(request.getPrice());
        }
        if (Objects.nonNull(request.getSeckillingStock())) {
            builder.seckillingStock(request.getSeckillingStock());
        }
        activitySkuRefMapper.updateById(builder.build());
    }

    @Override
    public void modify(Long activityId, Long skuId, Integer salableStock, Integer lockedStock, Integer saledStock) {
        QueryWrapper<ActivitySkuRef> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id", activityId);
        queryWrapper.eq("sku_id", skuId);
        ActivitySkuRef activitySkuRef = ActivitySkuRef.builder()
                .salableStock(salableStock)
                .lockedStock(lockedStock)
                .saledStock(saledStock)
                .build();
        activitySkuRefMapper.update(activitySkuRef, queryWrapper);
    }

    @Override
    public boolean updataPageReady(Long activityId, Long skuId, boolean pageReady) {
        ActivitySkuRef activitySkuRef = ActivitySkuRef.builder().pageReady(pageReady).build();
        QueryWrapper<ActivitySkuRef> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id", activityId);
        queryWrapper.eq("sku_id", skuId);
        int affectedRows = activitySkuRefMapper.update(activitySkuRef, queryWrapper);
        return affectedRows == 1;
    }

    @Override
    public void remove(Long id) {
        activitySkuRefMapper.deleteById(id);
    }

    @Override
    public void batchRemove(List<Long> ids) {
        activitySkuRefMapper.deleteBatchIds(ids);
    }
}
